local weidang = fk.CreateSkill{
  name = "xiaobai__weidang"
}

Fk:loadTranslationTable{
  ["xiaobai__weidang"] = "巍荡",
  [":xiaobai__weidang"] = "出牌阶段，你可以重铸两张牌（不能是此阶段进入过弃牌堆的花色），"..
    "视为对一名其他角色使用【桃】或【决斗】，然后其可以对你也如此做或将手牌摸至上限。",
  
  ["@weidang-turn"] = "巍荡"
}

local U = require "packages.utility.utility"

weidang:addEffect("active", {
  interaction = function (self, player)
    local all_choices = {"peach", "duel"}
    local pe,d = Fk:cloneCard("peach"), Fk:cloneCard("duel")
    pe.skillName = weidang.name
    d.skillName = weidang.name
    local choices = {}
    if not player:prohibitUse(pe) then
      table.insertIfNeed(choices, "peach")
    end
    if not player:prohibitUse(d) then
      table.insertIfNeed(choices, "duel")
    end
    return UI.CardNameBox{
      choices = choices,
      all_choices = all_choices
    }
  end,   
  target_filter = function (self, player, to_select, selected, selected_cards, card, extra_data)
    if #selected > 0 or to_select == player then return end
    if self.interaction.data == "peach" or self.interaction.data == "duel" then
      local c = Fk:cloneCard(self.interaction.data)
      c.skillName = weidang.name
      return player:canUseTo(c, to_select, {bypass_distances = false, bypass_times = true})
    end
  end,
  card_filter = function (self, player, to_select, selected, selected_targets)
    if #selected >= 2 then return end
    return not table.contains(player:getTableMark("@weidang-turn"), U.ConvertSuit(Fk:getCardById(to_select).suit, "int", "sym"))
  end,
  feasible = function (self, player, selected, selected_cards, card)
    return #selected_cards == 2 and #selected == 1
  end,
  on_use = function (self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    room:recastCard(effect.cards, player, weidang.name)
    if player.dead then return end  
    local card = Fk:cloneCard(self.interaction.data)
    card.skillName = weidang.name
    room:useCard{
      from = player,
      tos = effect.tos,
      card = card
    }
    if player.dead then 
      if target:getHandcardNum() < target:getMaxCards() then
        target:drawCards(target:getMaxCards() - target:getHandcardNum(), weidang.name)
      end
    else
      local c = Fk:cloneCard(self.interaction.data)
      c.skillName = weidang.name  
      local all_choices = {"xiaobai__weidang-drawCards", "xiaobai__weidang-useCard"}
      local able = {target:getHandcardNum() < target:getMaxCards(), c.name ~= "peach" or target:canUseTo(c, target)}
      local choices = table.filter (all_choices, function (_, index)
        return able[index]
      end)
      if #choices == 0 then return end
      local choice = room:askToChoice(target, {
        choices = choices,
        all_choices = all_choices,
        skill_name = weidang.name
      })
      if choice == "xiaobai__weidang-drawCards" then
        target:drawCards(target:getMaxCards() - target:getHandcardNum(), weidang.name)
      else
        room:useCard{
          from = target,
          tos = {player},
          card = c
        }
      end
    end
  end
})

weidang:addEffect(fk.AfterCardsMove, {
  can_refresh = function(self, event, target, player, data)
    if player:hasSkill(weidang.name, true) and #player:getTableMark("@weidang-turn") < 4 then
      for _, move in ipairs(data) do
        if move.toArea == Card.DiscardPile then
          return true
        end
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local mark = player:getTableMark("@weidang-turn")
    for _, move in ipairs(data) do
      if move.toArea == Card.DiscardPile then
        for _, info in ipairs(move.moveInfo) do
          table.insertIfNeed(mark, Fk:getCardById(info.cardId):getSuitString(true))
        end
      end
    end
    table.removeOne(mark, "log_nosuit")
    player.room:setPlayerMark(player, "@weidang-turn", mark)
  end,
})

weidang:addAcquireEffect(function (self, player, is_start)
  if not is_start then
    local room = player.room
    local suits = {}
    room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function (e)
      for _, move in ipairs(e.data) do
        if move.toArea == Card.DiscardPile then
          for _, info in ipairs(move.moveInfo) do
            table.insertIfNeed(suits, Fk:getCardById(info.cardId):getSuitString(true))
          end
        end
      end
    end, Player.HistoryTurn)
    table.removeOne(suits, "log_nosuit")
    if #suits > 0 then
      room:setPlayerMark(player, "@weidang-turn", suits)
    end
  end
end)

weidang:addLoseEffect(function (self, player, is_death)
  player.room:setPlayerMark(player, "@weidang-turn", 0)
end)

return weidang